package com.yunshipei.ui.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yunshipei.base.BaseActivity;
import com.yunshipei.base.BaseSubscriberAdapter;
import com.yunshipei.common.manager.EnterConfig;
import com.yunshipei.common.net.HttpMethods;
import com.yunshipei.common.wedigt.YspTitleBar;
import com.yunshipei.common.wedigt.YspButton;
import com.yunshipei.enterplorer.R;
import com.yunshipei.manager.YspPreferences;
import com.yunshipei.ui.dialog.DialogHelper;
import com.yunshipei.ui.dialog.WaitDialog;

import org.json.JSONObject;

import java.util.Map;

import io.rong.eventbus.EventBus;

//import de.greenrobot.event.EventBus;

/**
 * Created by cike on 15/12/22.
 */
public abstract class FindPaasswardBaseActivity extends BaseActivity {

    public FrameLayout frameLayout = null;
    public TextView findPassStatus = null;
    public YspButton next = null;
    public SharedPreferences sharedPreferences = null;
    public SharedPreferences.Editor editor = null;
    private WaitDialog mLoadingDialog = null;
    private YspTitleBar mTitleBar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.find_pass_base_layout);
        sharedPreferences = YspPreferences.getInstance().getSharedPreferences();
        editor = sharedPreferences.edit();
        bindView();
    }

    private void bindView() {
        frameLayout = (FrameLayout) findViewById(R.id.de_content);
        findPassStatus = (TextView) findViewById(R.id.find_pass_status);
        next = (YspButton) findViewById(R.id.next);
        next.setBtnText("下一步");
        mTitleBar = (YspTitleBar) findViewById(R.id.title_bar);
        next.setTopTvClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nextClickListener();
            }
        });

    }

    public void addContentView(View contentView) {
        frameLayout.addView(contentView);
    }

    /**
     * 网络请求
     *
     * @param port
     * @param map
     */
    public void network(String port, Map<String, String> map) {
        HttpMethods.getInstance().retrievePassword(port, map, new BaseSubscriberAdapter<JSONObject>() {

            @Override
            public void onNext(JSONObject jsonObject) {
                onNetworkSuccess(jsonObject, "");
            }

            @Override
            public void onError(String errorMessage) {
                onNetworkFailure(errorMessage);
            }
        });
    }

    /**
     * next click listener
     */
    public abstract void nextClickListener();

    /**
     * 网络请求成功
     *
     * @param jsonObject
     * @param msg
     */
    public abstract void onNetworkSuccess(JSONObject jsonObject, String msg);

    /**
     * 网络请求失败
     *
     * @param errorMsg
     */
    public abstract void onNetworkFailure(String errorMsg);


    /**
     * 设置找回密码状态颜色
     *
     * @param statusColor
     */
    public void setFindPassStatusColor(int statusColor) {
        switch (statusColor) {
            case 1:
                setColorAndtitle(7, "输入用户名");
                break;
            case 2:
                setColorAndtitle(15, "输入验证码");
                break;
            case 3:
                setColorAndtitle(findPassStatus.getText().toString().length(), "设置新密码");
                break;
            default:
                break;
        }
    }

    private void setColorAndtitle(int endIndex, String titleStr) {
        SpannableStringBuilder builder = new SpannableStringBuilder(findPassStatus.getText().toString());
        int color = EnterConfig.getInstance().getGlobalColor();
        ForegroundColorSpan redSpan = new ForegroundColorSpan(color);
        builder.setSpan(redSpan, 0, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        findPassStatus.setText(builder);
        mTitleBar.setTitle(titleStr);
    }

    public void showToast(String contentStr) {
        Toast.makeText(FindPaasswardBaseActivity.this, contentStr, Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
        super.onDestroy();
    }

    protected void showDialog(String dialogContext) {
        if (mLoadingDialog == null) {
            mLoadingDialog = DialogHelper.getWaitDialog(this, dialogContext);
        }
        mLoadingDialog.show();
    }

    protected void hideDialog() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

    public class EditTextWatcher implements TextWatcher {
        EditText editText;

        public EditTextWatcher(EditText editText) {
            this.editText = editText;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (editText.getText().toString().equals("")) {
                next.setAlpha(0.6f);
                next.setEnabled(false);
            } else {
                next.setEnabled(true);
                next.setAlpha(1f);
            }
        }
    }
}
